use moka::{sync::Cache};
use std::sync::OnceLock;

pub trait CacheType {
    fn get(key: &str) -> Option<String>;

    fn set(key: String, value: String);

    fn del(key: &str);

    fn get_cache() -> &'static Cache<String, String>;
}

static GLOBAL_CACHE: OnceLock<Cache<String, String>> = OnceLock::new();
pub struct GlobalCache;
impl CacheType for GlobalCache {
    
    fn get_cache() -> &'static Cache<String, String> {
        GLOBAL_CACHE.get_or_init(|| Cache::builder().max_capacity(1000).build())
    }

    fn get(key: &str) -> Option<String> {
        Self::get_cache().get(key)
    }

    fn del(key: &str) {
        Self::get_cache().remove(key);
    }

    fn set(key: String, value: String) {
        Self::get_cache().insert(key, value);
    }
}
